package com.example.hellodemo;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ProgressActivity extends AppCompatActivity {
    private ProgressBar progress3;
    private Button progressBtn, progressDialogBtn1,progressDialogBtn2;
    /**
     *      定义一个按钮 点击实现 从 0 到100 进度加载的过程
     * */

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        progress3 = findViewById(R.id.progress_3);
        progress3.setProgress(40);

        progressBtn = findViewById(R.id.progress_btn);
        progressBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hander.sendEmptyMessage(0);
            }
        });

        progressDialogBtn1 = findViewById(R.id.progress_dialog_btn1);
        progressDialogBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog dialog = new ProgressDialog(ProgressActivity.this);
                dialog.setTitle("进度加载");
                dialog.setMessage("请稍等");
                dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        Toast.makeText(ProgressActivity.this,"Cancle",Toast.LENGTH_LONG);
                    }
                });
                dialog.setCancelable(false); // 取消遮罩点击事件
                dialog.show();

            }
        });

        progressDialogBtn2 = findViewById(R.id.progress_dialog_btn2);
        progressDialogBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog dialog = new ProgressDialog(ProgressActivity.this);
                dialog.setTitle("进度提示");
                dialog.setMessage("正在下载");
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                dialog.show();
            }
        });


    }

        // 通过创建匿名类来 来快速实现 重载方法 实现代理等。
    Handler hander = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (progress3.getProgress() < 100){
                hander.postDelayed(runnable,500);
            }
        }
    };

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            progress3.setProgress(progress3.getProgress() + 5);
            hander.sendEmptyMessage(0);
        }
    };
}
